package com.vcms.utility.loader.storage.engine.xml.impl;

import org.w3c.dom.Element;

import com.vcms.entity.store.item.DrinksStoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.exception.VMCSException;
import com.vcms.utility.loader.storage.engine.xml.XmlStorageEngine;

/**
 * @author robin.foe A0092657U
 * Bridge design pattern concrete implementor
 */
public class DrinkXmlStorageEngine extends XmlStorageEngine<DrinksStoreItem>{

	public static final String ELEMENT_TAG_BRAND = "brand";
	public static final String ELEMENT_TAG_NAME = "name";
	public static final String ELEMENT_TAG_PRICE = "price";
	public static final String ELEMENT_TAG_QUANTITY = "quantity";
	
	public DrinkXmlStorageEngine(String fileName) throws VMCSException {
		super(fileName,ELEMENT_TAG_BRAND);
	}

	@Override
	public DrinksStoreItem getItem(int pk) {
		Element element = super.getElementByPk(pk);
		if(element == null)	return null;
		
		DrinksBrand drinkBrand = new DrinksBrand();
		drinkBrand.setName(super.getValue(element, ELEMENT_TAG_NAME));
		drinkBrand.setPrice(Integer.parseInt(super.getValue(element, ELEMENT_TAG_PRICE) ));
		return new DrinksStoreItem(drinkBrand, Integer.parseInt( super.getValue(element, ELEMENT_TAG_QUANTITY) ));
	}

	@Override
	public void setItem(int pk, DrinksStoreItem storeItem) {
		Element element = super.getElementByPk(pk);
		if(element == null) return;
		
		DrinksBrand drinkBrand = (DrinksBrand) storeItem.getContent();
		element.getElementsByTagName(ELEMENT_TAG_NAME).item(0).setTextContent(drinkBrand.getName());
		element.getElementsByTagName(ELEMENT_TAG_PRICE).item(0).setTextContent(String.valueOf(drinkBrand.getPrice()));
		element.getElementsByTagName(ELEMENT_TAG_QUANTITY).item(0).setTextContent(String.valueOf(storeItem.getQuantity()));
	}

}
